$(function(){
    $("#list").mothinDatagrid({
        url:'system/role!query.action',
        sortName: 'roleName',
        sortOrder: 'asc',
        columns:[[
            {field:'roleName',title:'角色名称',width:120,searchcolumn:"string",fixed:true,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'memo',title:'角色描述',width:400,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'id',title:'操作',width:0,fixed:true,resizable:false,formatter:function(value,row){
            	var data=frame_queryOperation(History.getState().url);
            	if(data&&data.rows&&data.rows.length>0){
            		var link="";
            		if(isLoginUserSuper()){
            			link+='<button class="btn btn-link btn-xs" onclick="handle(\'\',\'system/role!configPermission.action\',\'权限配置\',\''+value+'\');"><span class="glyphicon glyphicon-road" />&nbsp;权限配置</button>';
            		}
            		for(var i=0;i<data.rows.length;i++){
            			if(!data.rows[i].name){
            				continue;
            			}
            			link+='<button class="btn btn-link btn-xs" onclick="handle(\''+data.rows[i].id+'\',\''+data.rows[i].action+'\',\''+data.rows[i].name+'\',\''+value+'\');"><span class="'+data.rows[i].ico+'" />&nbsp;'+data.rows[i].name+'</button>';
            		}
            		return link;
            	}
            }}
        ]]
    },handle);
});

function handle(id,action,name,bizId){
	switch(action){
		case 'system/role!add.action':
			showDialog(action,'添加新角色',800,400,function(){
				$("#list").datagrid("reload");
			});
			break;
		case 'system/role!delete.action':
			showConfirm('确认删除此角色吗？',function(){
				$.ajax({
		    		type: 'POST',
		            url: action,
		            data: {'systemRole.id':bizId},
		            success: function(data){
		            	if(data.ok){
		            		showMessage('角色删除成功',function(){
		            			$("#list").datagrid("reload");
		            		});
		    	    	}else{
		    	    		showMessage('角色删除失败：'+data.message,null,'error');
		    	    	}
					}
				});
			});
			break;
		case 'system/role!update.action':
			var url=action+'?systemRole.id='+bizId;
			showDialog(url,'角色信息修改',800,400,function(){
				$("#list").datagrid("reload");
			});
			break;
		case 'system/role!configPermission.action':
			var url=action+'?systemRole.id='+bizId;
			showDialog(url,'角色权限配置改',600,600);
			break;
		case 'system/role!permission.action':
			var url=action+'?systemRole.id='+bizId;
			showDialog(url,'角色权限配查看',600,600);
			break;
	}
}